<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>在线考试</title>
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    <script src="http://47.106.66.89:8080/js/vue.min.js"></script>
    <script src="http://47.106.66.89:8080/js/axios.min.js"></script>
    <link href="http://47.106.66.89:8080/js/ele-2.15.7/theme-chalk/index.css" rel="stylesheet">
    <script src="http://47.106.66.89:8080/js/ele-2.15.7/index.js"></script>
</head>
<body>
<div id="app">
    <header>
        <div class="c900 flex">
            <a href="./"><h1 style="color: #fdfdfd">在线考试</h1></a>
            <span class="flex">
                <a href="register.html">注册</a>
                <a href="login.html">登录</a>
            </span>
        </div>
    </header>
    <main class="c900">
        <h1 style="width: 40%;margin: 20px auto">登录平台</h1>
        <form class="grid2">
            <label for="">账号:</label><input v-model="user.name" type="text">
            <span class="err" v-html="msgs.name"></span>
            <label for="">密码:</label><input v-model="user.pwd" type="password">
            <span class="err" v-html="msgs.pwd"></span>
            <div style="grid-column: 2; text-align: end">
                <a href="register.html" style="margin-right: 20px">注册</a>
                <button @click.prevent="login" style="padding: 5px 20px">登录</button>
            </div>
        </form>
    </main>
</div>
<script>
    var v = new Vue({
        el : "#app",
        data : {
            user:{name:"",pwd:""},
            msgs:{name:"",pwd:""},
        },
        created() {
            //初始化数据模型, 没有渲染到页面
        },
        mounted() {
            // 完成第一次渲染到页面
            if(location.search == "?1"){
                this.$alert("请先登录系统!");
            }
        },
        updated() {
            // 第N次渲染, 每次数据模型改变, 都会触发该钩子函数
        },
        methods : {
            login(){
                axios.post("signIn",this.user).then(res=>{
                   this.msgs={name:"",pwd:""};
                    if(res.data.code ==1){
                        this.$message(res.data.msg);
                        if(res.data.data){
                            location.href = res.data.data;
                        } else {
                            location.href = "index.html";
                        }
                    } else {
                        // TODO 请实现错误信息的展示
                        res.data.data.forEach(err=>{
                            if(this.msgs[ err.field ]){
                                this.msgs[ err.field ] += "<br>"
                            }
                            this.msgs[ err.field ] += err.defaultMessage;
                        });


                    }
                })
            }
        }
    })
</script>

<style>
    *{
        color:#222
    }
    .c900>form{
        width: 40%;
        margin: auto;
        padding: 30px;
        border: 1px green solid;
        border-radius: 10px;
        background-color: lightblue;
    }
    .c900>form label,.c900>form input{
        text-align: left;
        box-sizing: border-box;
        height: 35px;
        display: flex;
        align-items: center;
        outline: none;
        padding: 10px;
    }

    html,body,#app{
        height: 100%;
    }
    body{
        margin: 0px;
        padding: 0px;
        background-size: cover;
        background-image: url("https://img0.baidu.com/it/u=668574498,1720392676&fm=253&fmt=auto&app=138&f=JPEG?w=658&h=295");
    }
    a{
        text-decoration: none;
        color: #666
    }
    img{
        width: 100%;
        object-fit: cover;
        border-radius: 5px;
    }
    .c900{
        width: 900px;
        margin-inline: auto;
    }
    .flex{
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
    }
    .grid2{
        display: grid;
        grid-gap: 10px;
        grid-template-columns: auto auto;
    }
    .grid3{
        display: grid;
        grid-template-columns: auto auto auto;
    }
    .grid4{
        display: grid;
        grid-template-columns: auto auto auto auto;
    }
    #app>header{
        background-color: #0f9ae0;
        background-size: cover;
        position: sticky;
        top: 0px;
        z-index: 1000;
        height: 60px;
    }
    #app>header h1{
        margin: 10px;
    }
    #app>header span>a{
        margin-left: 20px;
        color: #fff;
    }
    #app>main{
        background-color: #eee8;
        min-height: calc(100% - 100px);
        padding: 20px;
    }
    .err{
        grid-column: 2;
        color: red
    }
</style>
</body>
</html>